package com.aistar.controller;

import com.aistar.mapper.QuestionMapper;
import com.aistar.pojo.*;
import com.aistar.service.QuestionService;
import com.aistar.service.TravelnoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class TravelSaveController {
    //前端接收游记的数据添加到数据库

    @Autowired
    private TravelnoteService travelnoteService;
    @Autowired
    private QuestionService questionService;




    @PostMapping("write/travelnote")
    public int saveTravelnote(HttpServletRequest request){

    /*    travelnote.setTravWid(1);//用户id
        travelnote.setTravStatus(1);
        travelnote.setTravLooknumber(0);//点赞数
        travelnote.setTravPraise(0);//浏览数
        travelnote.setTravTime(new Date());

        System.out.println(travelnote.getTravTitle());

        System.out.println(travelnote);*/



        Travelnote travelnote = new Travelnote();
        //接收前端的参数
        String traveltitel = request.getParameter("traveltitel");//获取标题
        travelnote.setTravTitle(traveltitel);



        String person = request.getParameter("person");//人物关系
        travelnote.setTravType(person);

        String cmbProvince = request.getParameter("cmbProvince");//省份
        String cmbCity = request.getParameter("cmbCity");//市级
        travelnote.setTravLoction(cmbProvince);

        String cmbArea = request.getParameter("cmbArea");//县级

        String img  = request.getParameter("img");//头图路径
        travelnote.setTravPic(img);

        String textall  = request.getParameter("textall");//带标签的全部富文本数据

        if(textall == null){
            textall = "没有信息哦~~";
        }
        travelnote.setTravText(textall);


        String textsx  = request.getParameter("textsx");//富文本前86个数据用来渲染首页的游记列表
        travelnote.setTravOther1(textsx);


        Float peoplemoeny  = Float.valueOf(request.getParameter("peoplemoeny")) ;//人均费用
        travelnote.setTravCost(peoplemoeny);

        Integer gooutday = Integer.valueOf(request.getParameter("gooutday"));//出行天数
        travelnote.setTravDay(gooutday);

        String aaa = request.getParameter("traveldate");//出行日期
        //将String转为Date类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date traveldate = new Date();
        try {
             traveldate = sdf.parse(aaa);//出行日期转为Date类型
        } catch (ParseException e) {
            e.printStackTrace();
        }

        travelnote.setTravDate(traveldate);


        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        travelnote.setTravWid(userId);
        travelnote.setTravStatus(1);
        travelnote.setTravLooknumber(0);//浏览数量
        travelnote.setTravPraise(0);//点赞数量

        Date nowdate = new Date();//获取当前时间
        travelnote.setTravTime(nowdate);

       int Y = travelnoteService.saveOneTravel(travelnote);
     return 1;


    }

    @PostMapping("write/question")
    public int saveQues(Question question, HttpServletRequest request){

  // Integer memberId =(Integer) request.getSession().getAttribute("userId");
        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        question.setQueUserId(userId);



    int Y = travelnoteService.saveOneQues(question);


    return Y;


    }

    @PostMapping("write/tracomm")
    public int savecomm(HttpServletRequest request){
        Tracomm tracomm  = new Tracomm();



        String replt =request.getParameter("replt"); //获取评论的内容
        tracomm.setTracOther1(replt);
        tracomm.setTracText(replt);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));

        int memberid = userId;
        tracomm.setTracUserId(memberid);

        String cc = request.getParameter("traid");//获取游记表的id
        int traid =Integer.valueOf(cc);
        tracomm.setTracTableId(traid);

        Date date = new Date();//获取当前时间
        tracomm.setTracTime(date);

        tracomm.setTracStatus(1);
        tracomm.setTracParise(0);

        System.out.println(tracomm);

      int V = travelnoteService.savetracomm(tracomm);



        return V;

    }

    @PostMapping("write/quesanswer")
    public int quesanswer(HttpServletRequest request){
        Answer answer = new Answer();

        String title =request.getParameter("title"); //获取评论的内容
        answer.setAnsText(title);

        String aa =request.getParameter("memberid"); //用户id
        int memberid = Integer.valueOf(aa);
        answer.setAnsUserId(memberid);

        String dd =request.getParameter("ansid"); //获得问表id
        int ansid = Integer.valueOf(dd);
        answer.setAnsQueId(ansid);

        answer.setAnsStatus(1);
        answer.setAnsTime(new Date());

        int Y = questionService.writeanswer(answer);


        return Y;
    }

}
